package ner;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Scorer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	}
	
	class StringStringMap {
		String word;
		String tag;
		public StringStringMap(String word, String tag) {
			this.word = word;
			this.tag = tag;
		}
	}
	
	public void calculateScore(String inputFile, String keyFile) {
		try {
			BufferedReader readerI = new BufferedReader(new FileReader(inputFile));
			BufferedReader readerK = new BufferedReader(new FileReader(keyFile));
			ArrayList<StringStringMap> keys = new ArrayList<Scorer.StringStringMap>();
			ArrayList<StringStringMap> input = new ArrayList<Scorer.StringStringMap>();
			
			String line;
			while((line = readerI.readLine())!=null) {
				String[] split = line.split(" ");
				input.add(new StringStringMap(split[1], split[0]));
			}
			
			while((line = readerK.readLine())!=null) {
				String[] split = line.split(" ");
				keys.add(new StringStringMap(split[1], split[0]));
			}
			
			int i = 0;
			int k = 0;
			int count = 0;
			while(i < input.size() &&  k < keys.size()) {
				StringStringMap inputWord = input.get(i);
				StringStringMap keyWord = keys.get(k);
				
				if(inputWord.word.equalsIgnoreCase(keyWord.word)) {
					if(inputWord.tag.equalsIgnoreCase(keyWord.tag))
						++count;
					i++;
					k++;
				}
				else {
					if (inputWord.word.indexOf(keyWord.word) >= 0) {
						k++;
						if(inputWord.word.endsWith(keyWord.word)) {
							i++;
						}
					}
					else {
						i++;
						if(keyWord.word.endsWith(inputWord.word)) 
							k++;
					}
				}
			}
			
			System.out.println("matched:"+count);
			System.out.println("Max possible:" + Math.max(input.size(), keys.size()));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
